package com.enlorenz.common.geo.analyzer.railspeed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.enlorenz.common.geo.base.IObject;
import com.enlorenz.common.geo.base.IRail;
import com.enlorenz.common.geo.base.IStatus;
import com.enlorenz.common.geo.util.GeographyUtil;
import com.enlorenz.util.DataDetUtil;

/**
 * 围栏速度分析
 * @author Administrator
 */
public abstract class AbstractRailSpeedAnalyzer {

	/**
	 * 获取物体的轨迹列表
	 * @param objectId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	protected abstract List<IStatus> getStatusList(String objectId,
			String startTime, String endTime);

	/**
	 * 获取围栏列表
	 * @param object
	 * @return
	 */
	protected abstract List<IRail> getRailList(IObject object);

	public List<RailSpeedData> getWorksAreaInfoByCldh(IObject object,
			String startTime, String endTime, int sp,
			Map<String, List<IStatus>> cache) {
		
		List<IRail> railList = getRailList(object);
		if (DataDetUtil.isCollectionEmpty(railList)) {
			return null;
		}
		
		String saveKey = object.getObjectId() + startTime + endTime;
		List<IStatus> pList = cache.get(saveKey);
		if (null == pList) {
			//获取指定时间的所有位置
			pList = getStatusList(object.getObjectId(), startTime, endTime);
			cache.put(saveKey, pList);
		}

		IStatus preStatus = null;
		Map<String, RailSpeedData> data = new HashMap<String, RailSpeedData>();
		for (IStatus status : pList) {
			if (null == preStatus) {
				preStatus = status;
				continue;
			}

			for (IRail rail : railList) {
				boolean isPreIn = GeographyUtil.pointIsInArea(rail, preStatus);
				boolean isIn = GeographyUtil.pointIsInArea(rail, status);

				if (isPreIn && isIn) {
					Integer sd = Integer.valueOf(preStatus.getSpeed());
					Double dis = GeographyUtil.getDistance(preStatus, status);
					Long time = PublicUtil.dateDiffer(preStatus
							.getStatusDateStr(), status.getStatusDateStr());

					String makeKey = rail.getRailId();
					RailSpeedData wrapper = data.get(makeKey);
					if (null == wrapper) {
						wrapper = new RailSpeedData();
						wrapper.setId(makeKey);
						
						data.put(makeKey, wrapper);
					}
					wrapper.setDis(wrapper.getDis() + dis);
					wrapper.setTime(wrapper.getTime() + time);

					if (sd >= 0 && sd < sp) {
						wrapper.setSp1(wrapper.getSp1() + dis.intValue());
					} else if (sd >= sp) {
						wrapper.setSp2(wrapper.getSp2() + dis.intValue());
					}
				}
			}
			preStatus = status;
		}

		/*TimeWrapper topest = null;
		for (String key : data.keySet()) {
			TimeWrapper tw = data.get(key);
			if (null == topest) {
				topest = tw;
				continue;
			}

			if (tw.getTime() > topest.getTime()) {
				topest = tw;
			}
		}

		if (null == topest) {
			return null;
		} else {
			// 如果停留5分钟
			if (topest.getTime() > 5 * 60 * 1000) {
				return topest;
			} else {
				return null;
			}
		}*/

		return new ArrayList(data.values());
	}
	
/*	public static class LineDataTimeExcute extends AbstractTimerEngine{

		@Override
		protected void doProcess() throws Exception {
			Calendar c = Calendar.getInstance();
			int day = c.get(Calendar.DATE);
	        c.set(Calendar.DATE, day - 1);
	        String dayBefore = new SimpleDateFormat("yyyyMMdd").format(c.getTime());
	        String dayNow = new SimpleDateFormat("yyyyMMdd").format(new Date());
	        
	        String dayBefore ="20120822";
	        String dayNow = "20120823";
	        
	        List<Dept> deptList=getDeptList();
	        //速度列表
	        int [] spArr={10,15,20,25,30,35};
	        
	        String jcSt=dayBefore+"223000";
	        String jcEd=dayNow+"030000";
	        Map<String,List<Position>> cache=new HashMap<String, List<Position>>();
	        for(Dept dept:deptList){
	        	for(int sp:spArr){
	        		System.out.println(jcSt+":"+jcEd+":"+sp+":"+dept.getN_jgdh());
	        		List<TimeWrapper> list=makeLineList(jcSt, jcEd, sp, dept.getN_jgdh(),cache);
	        		String makeKey=makeWorkLineKey(jcSt, jcEd, sp, dept.getN_jgdh());
	        		try{
	        			workCacheDataManage.saveData(makeKey, list);
	        		}catch (Exception e) {
						e.printStackTrace();
					}
	        	}
	        }
	        
	        cache=new HashMap<String, List<Position>>();
	        String pxSt=dayNow+"040000";
	        String pxEd=dayNow+"063000";
	        for(Dept dept:deptList){
	        	for(int sp:spArr){
	        		System.out.println(pxSt+":"+pxEd+":"+sp+":"+dept.getN_jgdh());
	        		List<TimeWrapper> list=makeLineList(pxSt, pxEd, sp, dept.getN_jgdh(),cache);
	        		String makeKey=makeWorkLineKey(pxSt, pxEd, sp, dept.getN_jgdh());
	        		try{
	        			workCacheDataManage.saveData(makeKey, list);
	        		}catch (Exception e) {
						e.printStackTrace();
					}
	        	}
	        }
		}

		@Override
		protected void onExceptionHappened(Exception arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void onFinallyDo() {
			// TODO Auto-generated method stub
			
		}
		
	}*/

}
